package com.pig4cloud.pig.common.swagger.enums;

import cn.hutool.core.lang.Dict;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.Optional;

import static java.util.Optional.ofNullable;
import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import static org.springframework.web.reactive.function.server.ServerResponse.status;

/**
 * swagger api 放行路径
 */
@Getter
@AllArgsConstructor
public enum SwaggerUrl {

    SWAGGER_RESOURCES_CONFIGURATION_SECURITY {
        @Override
        public <T> Mono<ServerResponse> createMono(Dict t) {
            return status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
                    .body(fromValue(ofNullable(t.getBean("uiConfiguration")).orElse(UiConfigurationBuilder.builder().build())));

        }
    },
    SWAGGER_RESOURCES_CONFIGURATION_UI {
        @Override
        public <T> Mono<ServerResponse> createMono(Dict t) {
            return status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON)
                    .body(fromValue(ofNullable(t.getBean("securityConfiguration")).orElse(SecurityConfigurationBuilder.builder().build())));
        }
    },
    SWAGGER_RESOURCES {
        @Override
        public <T> Mono<ServerResponse> createMono(Dict t) {
            return status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(fromValue(t.getBean("swaggerResources")));
        }
    };


    /**
     * 创建返回对象
     * @param t
     * @param <T>
     * @return
     */
    public abstract <T> Mono<ServerResponse> createMono(Dict t);


}
